package com.syxp.pda.action.base;

import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.web.PagingBean;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.Profess;
import com.syxp.pda.entity.base.Station;
import com.syxp.pda.service.base.ProfessService;

public class ProfessAction extends BaseAction {
	@Resource
	private ProfessService professService;
	
	public ProfessService getProfessService() {
		return professService;
	}

	public void setProfessService(ProfessService professService) {
		this.professService = professService;
	}

	private int professID;//传入的参数
	
	private int start ;
	
	private int limit ;
	
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	private Profess profess; //注入Station实体类
	
	public int getProfessID() {
		return professID;
	}

	public void setProfessID(int professID) {
		this.professID = professID;
	}

	public Profess getProfess() {
		return profess;
	}

	public void setProfess(Profess profess) {
		this.profess = profess;
	}

	public String save(){ //保存
		try{
			System.out.println(profess) ;
			Profess s= professService.addService(profess) ;
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("profess.professID", String.valueOf(profess.getProfessID())) ;
			jobj.addProperty("profess.professCode", profess.getProfessCode()) ;
			jobj.addProperty("profess.professName", profess.getProfessName()) ;
			jobj.addProperty("profess.remarks", profess.getRemarks()) ;
			jobj.addProperty("success", true) ;
			this.jsonString=gson.toJson(jobj) ;
		}catch(Exception e) {
			e.printStackTrace() ;
		}
		return SUCCESS ;
	}
	
	public String load() {  //加载所有数据
		List<Profess> all=null;
		QueryFilter filter = new QueryFilter() ;
		filter.setPagingBean(new PagingBean(start,limit)) ;
		if(limit==0){
			all = professService.getDao().getAll();
		}else{
			 all = professService.getDao().getAll(filter);
		}
		
		JsonArray jarray=new JsonArray();
		JsonObject result = new JsonObject() ;
		for(Profess profess : all) {
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("professCode", profess.getProfessCode()) ;
			jobj.addProperty("professName", profess.getProfessName()) ;
			jobj.addProperty("remarks", profess.getRemarks()) ;
			jobj.addProperty("professID", String.valueOf(profess.getProfessID())) ;
			jarray.add(jobj) ;
		}
		if(limit==0){
			this.jsonString = gson.toJson(jarray) ;
		}else{
			result.addProperty("totalProperty",professService.getDao().getTotalProperty(filter)) ;
			result.add("result", jarray) ;
			result.addProperty("start", start);
			result.addProperty("limit", limit) ;
			this.jsonString = gson.toJson(result) ;
		}
		
		System.out.println(jsonString) ;
		return SUCCESS ;
	}
	
	public String delete() {  //删除
		try{
			professService.removeService(professID) ;
			this.jsonString="{success:true}" ;
		}catch(Exception e){
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		
		return SUCCESS ;
	}
	public String get()	{  //查看
		try{
			Profess p = professService.loadService(professID) ;
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("professID", p.getProfessID()) ;
			jobj.addProperty("professCode", p.getProfessCode()) ;
			jobj.addProperty("professName", p.getProfessName()) ;
			jobj.addProperty("remarks", p.getRemarks()) ;
			jobj.addProperty("success", true) ;
			this.jsonString = gson.toJson(jobj) ;
			System.out.println(jsonString) ;
		}catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String update() {
		try{
			professService.updateService(profess) ;
			this.jsonString = "{success:true}" ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
}
